
package puziran.party.services.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import puziran.party.dao.OperatorDao;
import puziran.party.dao.PartyDao;
import puziran.party.domain.Operator;
import puziran.party.domain.Party;
import puziran.party.services.UserOperationService;
import puziran.party.util.BeanMapUtils;
import puziran.party.util.JSONHelper;
import puziran.sofa.util.ParseFormToBean;

@Service("userOperationService")
public class UserOperationServiceImpl implements UserOperationService{
	/**
	 * 日志
	 */
	public static Logger logger = Logger.getLogger(UserOperationServiceImpl.class);
	
	/**
	 * 注入partyDao
	 */
	@Resource
	PartyDao partyDao;
	
	/**
	 * 注入operatorDao
	 */
	@Resource
	OperatorDao operatorDao;
	
	/**
	 * 
	 * TODO 初始化操作
	 * @see puziran.party.services.UserOperationService#initUser(java.util.Map)
	 */
	@SuppressWarnings("javadoc")
	@Override
	public String initUser(Map<String, String> map) {
		/**
		 * 初始化party
		 */
		Party party = new Party();
		ParseFormToBean pftb = new ParseFormToBean();
		party=(Party)pftb.parseToBean(map, party);
		String partyid = "";
		try {
			partyid = partyDao.insert(party);
		} catch (Exception e) {
			logger.info("insertParty:"+e.getMessage());
			return "sorry";
		}
		map.put("partyid", partyid);
		Operator operator = new Operator();
		operator=(Operator)pftb.parseToBean(map, operator);
		String operatorid = operatorDao.insert(operator);	
		
		Map<String,String> returnMap = new HashMap<String,String>();
		/**
		 * 返回初始化的partyid，operatorid
		 */
		returnMap.put("partyid",partyid);
		returnMap.put("operatorid",operatorid);
		
		return JSONHelper.Map2JSON(returnMap);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Map<String,String> vilidateUser(Map<String, String> map) {
		map.put("keywords", (String)map.get("username"));
		Party party = partyDao.selectPartyByKeywords(map);
		Map<String,String> resultMap = new HashMap<String,String>();
		if(party != null){
			try {
				resultMap = BeanMapUtils.convertBean(party);
			} catch (Exception e) {
				logger.info("vilidateUser:+convertBean"+e.getMessage());
				return null;
			} 
			Map<String, Object> operatorNeedMap = new HashMap<String, Object>();
			operatorNeedMap.put("partyid", party.getPartyid());
			operatorNeedMap.put("operator", (String)map.get("operator"));
			operatorNeedMap.put("password", (String)map.get("password"));
			Operator operator = operatorDao.selectOperatorByPartyIdAndPasswordAndOperator(operatorNeedMap);
			Map<String,String> operatorMap = null;
			if(operator != null){
				try {
					operatorMap = BeanMapUtils.convertBean(operator);
//					System.err.println(operator.getLastloginip());
//					System.err.println(map.get("lastloginip"));
					
					if(operator.getLastloginip().equals(map.get("lastloginip"))){
						resultMap.put("ip_warn", "no");//用于ip异常报警使用
					}else{
						resultMap.put("ip_warn", "yes");//用于ip异常报警使用
						resultMap.put("lastloginip",map.get("lastloginip"));//用于ip异常报警使用
						operatorMap.put("lastloginip",map.get("lastloginip"));
					}
					operatorMap.put("logintimes",Integer.parseInt(operator.getLogintimes())+1+"");
					operatorDao.updateOperartorByPartyIdAndOperator(operatorMap);//更新ip和登录次数
					return resultMap;
				} catch (Exception e) {
					logger.info("vilidateUser:+convertBean"+e.getMessage());
					return null;
				}
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	@Override
	public Map<String, String> isOrNotExist(Map<String, String> map) {
		map.put("keywords", (String)map.get("username"));
		Party party = partyDao.selectPartyByKeywords(map);
		Map<String,String> returnMap = new HashMap<String,String>();
		if(party!=null){
			returnMap.put("isexist", "yes");
		}else{
			returnMap.put("isexist", "no");
		}
		return returnMap;
	}
}

